package com.ocom.ugcapi.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ocom.common.entity.card.EntityXfRecord;
import com.ocom.common.resp.Result;
import com.ocom.common.vo.pay.CallBackVo;
import com.ocom.common.vo.payconfig.kitchenprinter.PlanKitchenPrinterRequest;
import com.ocom.redis.util.RedisUtil;
import com.ocom.ugcapi.client.PayConfigClientService;
import com.ocom.ugcapi.client.PayHandleClientService;
import com.ocom.ugcapi.client.XfRecodeClientService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022年7月12日09:02:39
 **/
@Slf4j
@Component
public class CallbackConsumer {


	@Autowired
	private XfRecodeClientService xfRecodeClientService;

	@Autowired
	private PayHandleClientService payHandleClientService;

	@Autowired
	private PayConfigClientService payConfigClientService;


	@Resource
	RedisUtil redisUtil;

	/**
	 * 请留意此处 监听的队列名称
	 */
	@RabbitHandler
	@RabbitListener(queues = "dead-callback-queue")
	public void listenLimitingQueue(Message message, @Headers Map<String, Object> headers,  Channel channel) throws IOException {

		try {
			System.out.println("限流队列接收到消息:" + message);

			Gson gson=new Gson();
			String se = new String(message.getBody(), "UTF-8");
			CallBackVo callBackVo= gson.fromJson(se,CallBackVo.class);


			payHandleClientService.callback(callBackVo);


			Result result =payHandleClientService.getOrderInfo(callBackVo.getCompanyId(),callBackVo.getPlatOrderNo(),"");

			//同步到成功记录
			if(result.getCode()==200){

				EntityXfRecord entityXfRecord=  JSONObject.parseObject(JSON.toJSONString( result.getData()), EntityXfRecord.class);
				entityXfRecord.setPayState(1);
				Result result1 = xfRecodeClientService.sveXfRecord(entityXfRecord);
				if(entityXfRecord.getConsType()==100 || entityXfRecord.getConsState()==3 || entityXfRecord.getConsState()==5){
					payHandleClientService.syncSaveGoodList(entityXfRecord.getComId(),entityXfRecord.getOrderNo());
				}
				log.info("记录到成功库 【{}】【{}】",callBackVo,result1);

//				if (200 == result1.getCode()) {
					try {

						if (3 == entityXfRecord.getConsState() || 5 == entityXfRecord.getConsState()) {
							log.info("开始打印订单 订单：【{}】，回调：【{}】",entityXfRecord,callBackVo);
							PlanKitchenPrinterRequest planKitchenPrinterRequest = new PlanKitchenPrinterRequest();
							planKitchenPrinterRequest.setComId(entityXfRecord.getComId());
							planKitchenPrinterRequest.setPointId(entityXfRecord.getPointId());
							planKitchenPrinterRequest.setOrderNo(entityXfRecord.getOrderNo());
							planKitchenPrinterRequest.setCheckPrint(true);

							log.info("开始打印订单 planKitchenPrinter：【{}】",planKitchenPrinterRequest);
							Result printResult = payConfigClientService.planKitchenPrinter(planKitchenPrinterRequest);
							log.info("订单打印结果【{}】",printResult);
						}

					}catch (Exception e) {
						e.printStackTrace();
						log.info("订单打印异常 【{}】【{}】",callBackVo,result1);
					}
//				}

			}else{
				//记录异常
				log.info("获取订单异常 【{}】【{}】",callBackVo,result);
			}

		}catch (Exception e){
			log.info("处理回调异常 【{}】" ,e);
		}

		//确认接收消息
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}
}
